/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javatarefasclient.ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

/**
 * Paulo Canedo 15/07/2009
 * http://www.paulocanedo.com.br
 *
 * Utilize livremente o codigo, mas por favor, nao retire os creditos :P
 *
 * @author paulocanedo
 */
public class BigButton extends JButton implements MouseListener, FocusListener {

    private enum STATE {

        MOUSE_OVER, MOUSE_PRESSED, NORMAL, FOCUSED
    };
    private String description;
    private Color borderColorOverOut = Color.DARK_GRAY;
    private STATE atualState = STATE.NORMAL;
    private STATE tempState = STATE.NORMAL;

    {
        addMouseListener(this);
        addFocusListener(this);
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    public BigButton() {
    }

    public BigButton(String text, Icon icon, String description) {
        super(text, icon);
        setDescription(description);
    }

    public BigButton(String text, Icon icon) {
        super(text, icon);
    }

    public BigButton(ImageIcon imageIcon, String description) {
        super(imageIcon);
        setDescription(description);
    }

    public BigButton(Action a) {
        super(a);
    }

    public BigButton(String text) {
        super(text);
    }

    public BigButton(Icon icon) {
        super(icon);
    }

    @Override
    public void setIcon(Icon defaultIcon) {
        super.setIcon(defaultIcon);
    }

    @Override
    protected void paintBorder(Graphics g) {
        int x = 4, y = 4;
        int w = getWidth() - 9, h = getHeight() - 9;

        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g2.setStroke(new BasicStroke(4f));
        g2.setColor(borderColorOverOut);
        g2.drawRoundRect(x, y, w, h, h / 3, h / 3);

        g2.setStroke(new BasicStroke(1.7f));
        g2.setColor(Color.WHITE);
        g2.drawRoundRect(x + 2, y + 2, w - 4, h - 4, h / 3, h / 3);

        g2.dispose();
    }

    @Override
    protected void paintComponent(Graphics g) {
        int x = 7, y = 7;
        int w = getWidth() - 11, h = getHeight() - 11;

        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        Action action = getAction();
        String text = getText();
        Icon icon = getIcon();
        if (text == null && action != null) {
            text = (String) action.getValue(Action.NAME);
        }
        if (icon == null && action != null) {
            icon = (Icon) action.getValue(Action.LARGE_ICON_KEY);
        }

        Color topColor;
        Color bottomColor;
        switch (atualState) {
            case MOUSE_OVER: {
                bottomColor = getBackground().brighter().brighter();
                topColor = getBackground();
                break;
            }
            case MOUSE_PRESSED: {
                topColor = new Color(0xaa4433);
                bottomColor = topColor.brighter().brighter();
                break;
            }
            case FOCUSED: {
                topColor = new Color(0x9999ca);
                bottomColor = topColor.brighter().brighter();
                break;
            }
            default: {
                topColor = getBackground().brighter().brighter();
                bottomColor = getBackground();
            }
        }

        GradientPaint gp = new GradientPaint(w / 2, 0, topColor, w / 2, h, bottomColor);
        g2.setPaint(gp);
        g2.fillRoundRect(x, y, w - 3, h - 3, getHeight() / 3, h / 3);

        if (icon != null) {
            icon.paintIcon(this, g2, 15, y + (h / 2) - (icon.getIconHeight() / 2));
        }
        g2.setColor(getForeground());

        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

        int titleHeightPosition = y;
        if (text != null) {
            titleHeightPosition = y + (h / 2) + (g2.getFontMetrics().getLeading() / 2);
          // g2.setFont(g2.getFont().deriveFont(Font.BOLD, 22));
            g2.drawString(text,(icon == null ? x : icon.getIconWidth()) +5,titleHeightPosition);
        }

        //g2.setFont(getFont().deriveFont(Font.PLAIN, 12));
        if (getDescription() != null) {
            g2.drawString(getDescription(),
                    (icon == null ? x : icon.getIconWidth()) + 25,
                    g2.getFontMetrics().getHeight() + titleHeightPosition);
        }

        g2.dispose();
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        atualState = STATE.MOUSE_PRESSED;
    }

    public void mouseReleased(MouseEvent e) {
        atualState = STATE.NORMAL;
        repaint();
    }

    public void mouseEntered(MouseEvent e) {
        tempState = atualState;
        atualState = STATE.MOUSE_OVER;
    }

    public void mouseExited(MouseEvent e) {
        atualState = tempState;
    }

    public void focusGained(FocusEvent e) {
        atualState = STATE.FOCUSED;
    }

    public void focusLost(FocusEvent e) {
        atualState = STATE.NORMAL;
    }
}
